import numpy as np

class GLDS:
    def __init__(self,NormalizeMatrix,Weight,Gangliang):
        '''

        :param NormalizeMatrix: 矩阵
        :param Weight: 权重
        :param Gangliang: 属性的成本或效益型
        '''
        self.Matrix=NormalizeMatrix
        self.Weight=Weight
        self.lines=len(NormalizeMatrix)
        self.colums=len(NormalizeMatrix[0])
        self.Ganglinag=Gangliang
        self.DS1=[]
        self.DS2=[]
        self.R1=[]
        self.R2=[]

    def getAttributeDominace(self,j,i,v):
        '''

        :param j: 第j个属性
        :param i: 第i个对象
        :param v: 第v个对象
        :return: 两个对象在第j个属性的优势值
        '''
        temp = 0
        if (self.Ganglinag[j] == 1):
            temp = self.Matrix[i][j] - self.Matrix[v][j]
        else:
            temp = self.Matrix[v][j] - self.Matrix[i][j]
        return temp

    def getSum(self,j):
        sumNorm = 0
        temp = 0
        for n in range(self.lines):
            for m in range(self.lines):
                temp = self.getAttributeDominace(n, m, j)
                temp = pow(temp, 2)
                sumNorm = sumNorm + temp
        sumNorm = pow(sumNorm, 1 / 2)
        return sumNorm

    def getDominanceflow(self,i,v,j):
        '''

        :param i:
        :param v:
        :param j:
        :return:
        '''
        sumNorm=0
        df=self.getAttributeDominace(i,v,j)
        sumNorm=self.getSum(j)
        sumNorm=df/sumNorm
        return sumNorm

    def getGainDominance(self):
        '''

        :return:
        '''
        for i in range(self.lines):
            cloumevalue=0
            for j in range(self.colums):
                sumvalue = 0
                for m in range(self.lines):
                    sumvalue=sumvalue+self.getDominanceflow(i,m,j)
                cloumevalue=cloumevalue+self.Weight[j]*sumvalue
            self.DS1.append(cloumevalue)
        return self.DS1

    def getR1(self):
        R1=[]
        R1=sorted(self.DS1,reverse=0)
        self.R1=R1
        return R1


    def getLostDominance(self):
        '''

        :return:
        '''
        for i in range(self.lines):
            maxattribue=[]
            for j in range(self.colums):
                maxdf=[]
                for v in range(self.lines):
                    temp=self.getDominanceflow(v,i,j)
                    maxdf.append(temp)
                maxdfvalue=max(maxdf)
                maxattribue.append(self.Weight[j]*maxdfvalue)
            tempvalue=max(maxattribue)
            self.DS2.append(tempvalue)
        return self.DS2

    def getR2(self):
        '''

        :return:
        '''
        R2=[]
        R2=sorted(self.DS2)
        self.R2=R2
        return R2

    def getGlobalsorre(self):
        '''

        :return:
        '''
        BS=[]
        sumD1=0
        sumD2=0
        self.getLostDominance()
        self.getGainDominance()
        self.getR1()
        self.getR2()
        for i in range(self.lines):
            sumD1=sumD1+pow(self.DS1[i],2)
            sumD2 = sumD2 + pow(self.DS2[i], 2)
        sumD1=pow(sumD1,1/2)
        sumD2=pow(sumD2,1/2)
        temp=0
        n=self.lines
        for i in range(n):
            temp=self.DS1[i]/sumD1
            temp=temp*(n-self.R1[i]+1)/(n*(n+1)/2)
            temp2=self.DS2[i]/sumD2
            temp2=temp2*(self.R2[i]/(n*(n+1)/2))
            BS.append(temp-temp2)

    def getResult(self):
      result=self.getGlobalsorre()
      return result













